using System;
using System.Collections.Generic;
using System.Text;
using MediaWikiEngine.Domain;
using MediaWikiEngine.Command;
using System.Collections;

namespace MediaWikiEngine
{
    /// <summary>
    /// Allows a controlling program to interactively perform automated functions
    /// against a MediaWiki installation.
    /// </summary>
    public class InteractiveCommand
    {
        WikiConnection _conn;

        /// <summary>
        /// Creates a new command runner with a provided connection to the wiki.
        /// </summary>
        /// <param name="connection">The <see cref="WikiConnection"/> object for the wiki being manipulated.</param>
        public InteractiveCommand(WikiConnection connection)
        {
            _conn = connection;
        }

        /// <summary>
        /// Retrieves a page from the wiki.
        /// </summary>
        /// <param name="ns">The namespace of the page, i.e. "Main" or "Category" etc.</param>
        /// <param name="title">The title of the page being retrieved.</param>
        /// <remarks>Once localization efforts are completed, the <paramref name="ns"/> parameter may either be the English version
        /// or the local version of the namespace name.</remarks>
        public GetPageResult GetPage(string ns, string title)
        {
            GetPage command = new GetPage(_conn, ns, title);

            return command.Execute() as GetPageResult;
        }

        /// <summary>
        /// Signs into the wiki.
        /// </summary>
        /// <param name="username">The username of a valid user on the wiki.</param>
        /// <param name="password">A plain-text password for the user.</param>
        public LoginResult Login(string username, string password)
        {
            Login command = new Login(_conn, username, password);

            return command.Execute() as LoginResult;
        }

        /// <summary>
        /// Saves a new or updated page to the wiki.
        /// </summary>
        /// <param name="pagedata">The <see cref="WikiPage"/> object containing the content being saved.</param>
        /// <param name="summary">An edit summary for the edit.</param>
        /// <param name="ns">The namespace of the page being saved; if blank, the namespace of the <paramref name="pagedata"/> will be used.</param>
        /// <param name="title">The title of the page being saved; if blank, the namespace of the <paramref name="pagedata"/> will be used.</param>
        /// <remarks>Once localization efforts are completed, the <paramref name="ns"/> parameter may either be the English version
        /// or the local version of the namespace name.</remarks>
        public SavePageResult SavePage(WikiPage pagedata, string summary, string ns, string title)
        {
            SavePage command = new SavePage(_conn, pagedata, summary, ns, title);

            return command.Execute() as SavePageResult;
        }

        /// <summary>
        /// Saves a new or updated page to the wiki. The namespace and page title will be the ones stored in <paramref name="pagedata"/>.
        /// </summary>
        /// <param name="pagedata">The <see cref="WikiPage"/> object containing the content being saved.</param>
        /// <param name="summary">An edit summary for the edit.</param>
        public SavePageResult SavePage(WikiPage pagedata, string summary)
        {
            SavePage command = new SavePage(_conn, pagedata, summary, string.Empty, string.Empty);

            return command.Execute() as SavePageResult;
        }

        /// <summary>
        /// Uploads a file to the wiki.
        /// </summary>
        /// <param name="path">The full path name to the file containing data to be uploaded.</param>
        /// <param name="mimetype">The mimetype of the data being uploaded.</param>
        /// <param name="description">A description of the file being uploaded.</param>
        /// <param name="destinationFilename">The resulting filename of the file on the wiki.</param>
        /// <param name="license">A license for the file; will not add the license parameter to upload if blank.</param>
        public UploadFileResult UploadFile(string path, string mimetype, string destinationFilename, string description, string license)
        {
            UploadFile command = new UploadFile(_conn, path, mimetype, destinationFilename, description, license);

            return command.Execute() as UploadFileResult;
        }

        /// <summary>
        /// Completely replaces the wiki markup of the <see cref="WikiPage"/> passed in <paramref name="page"/> with new markup.
        /// </summary>
        /// <param name="page">The page whose markup is being replaced.</param>
        /// <param name="content">The new wiki markup.</param>
        /// <remarks>Does not connect to the wiki. Equivalent to the (instance) method WikiPage.WikiMarkup = content;</remarks>
        public ReplaceContentResult ReplaceContent(WikiPage page, string content)
        {
            ReplaceContent command = new ReplaceContent(_conn, page, content);

            return command.Execute() as ReplaceContentResult;
        }

        /// <summary>
        /// Changes a page's watch status based on the Watch flag of the <see cref="WikiPage"/> parameter passed in <paramref name="page"/>.
        /// </summary>
        /// <param name="page">The wiki page whose watch status is being changed.</param>
        public UpdateWatchResult UpdateWatch(WikiPage page)
        {
            UpdateWatch command = new UpdateWatch(_conn, page);

            return command.Execute() as UpdateWatchResult;
        }

    }
}
